const router = require('koa-router')()
const debug = require('debug')('demo:server')

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!',
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json',
  }
})

// 获取get请求参数
router.get('/getParams', async (ctx, next) => {
  ctx.body = {
    title: 'get Params',
    params: ctx.request.query,
  }
})
// 获取post请求参数
router.post('/postParams', async (ctx, next) => {
  console.log(ctx.request.body)

  ctx.body = {
    title: 'post Params',
    params: ctx.request.body,
  }
})

router.get('/session-test', async (ctx, next) => {
  if (ctx.session.viewNum == null) {
    ctx.session.viewNum = 0
  } else {
    ctx.session.viewNum += 1
  }
  ctx.body = {
    title: 'get Params',
    params: ctx.request.query,
    viewNum: ctx.session.viewNum,
  }
})
module.exports = router
